package com.fcg.redis.orgtree.controller;

import com.fcg.redis.orgtree.bean.NodeInfoDto;
import com.fcg.redis.orgtree.service.OrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author FCG
 */
@RestController
public class OrgController {
    @Resource
    private OrgService orgService;

    /**
     * 查询指定层级下的所有结点
     *
     * @param nodeInfo level
     * @return 指定层级下的所有结点
     */
    @PostMapping("/levelTree")
    public Object getLevelTree(@RequestBody NodeInfoDto nodeInfo) {
        if (Objects.isNull(nodeInfo.getLevel())) {
            //查询根节点
            nodeInfo.setLevel(1);
        }
        return orgService.getSubTree(nodeInfo.getLevel(), null);
    }

    /**
     * 查询 某个节点的所有子节点
     *
     * @param nodeInfo nodeId
     *                 level
     * @return 某个节点的所有子节点
     */
    @PostMapping("/subtree")
    public Object getSubTree(@RequestBody NodeInfoDto nodeInfo) {
        if (Objects.isNull(nodeInfo.getLevel()) || StringUtils.isBlank(nodeInfo.getNodeId())) {
            return null;
        }
        return orgService.getSubTree(nodeInfo.getLevel() + 1, nodeInfo.getNodeId());
    }

    /**
     * 查询 某个节点的详细数据
     * @param nodeInfo nodeId
     * @return 某个节点的详细数据
     */
    @PostMapping("/node")
    public Object getNode(@RequestBody NodeInfoDto nodeInfo) {
        return orgService.getNode(nodeInfo.getLevel(), nodeInfo.getNodeId());
    }

}
